// components/search-bar/search-bar.js
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    tabMapper: {
      type: Object
    },
    onSearchInput: {
      type: Function
    },
    tabChange: {
      type: Function
    },
    tabs: {
      type: Array
      // value:[],
    },
    showTab: {
      type: Boolean,
      value: true
    },
    showSearch: {
      type: Boolean,
      value: true
    },

    tabKey: {
      type: String,
      value: ''
    },
    searchKey: {
      type: String,
      value: 'searchStr'
    },
    listQuery: {
      type: Function,
  
    }

  },

  /**
   * 组件的初始数据
   */
  data: {
    currentTab: 'ALL',
    searchInput: '',
  },
  lifetimes: {
    attached: function() {
      // 在组件实例进入页面节点树时执行
    },
    detached: function() {
      // 在组件实例被从页面节点树移除时执行
    },
    ready:function(){
    
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    bindSearchInput(e) {
      var val = e.detail.value;
      console.log(val);
      this.setData({
        searchInput: val,

      }, () => {
        this.buildQuery();
      })
    },
    buildQuery() {
      const queryParams = {};
      if (this.data.tabKey) {
        queryParams[this.data.tabKey] = this.data.tabMapper ? this.data.tabMapper[this.data.currentTab] : this.data.currentTab;
      }
      console.log("searchKey", this.data.searchKey)
      queryParams[this.data.searchKey] = this.data.searchInput;
      console.log('queryParams', queryParams)
      console.log("this.data.listQuery",this.data.listQuery)
      // if (this.data.listQuery) {
      //   this.data.listQuery(queryParams)
      // }
      this.triggerEvent('listQuery',queryParams)
    },
    onTabChange(e) {
      const tabItem = e.currentTarget.dataset.tabitem;
      // console.log("tab", tabItem)
      this.setData({
        currentTab: tabItem,

      }, () => {
        this.buildQuery()
      })
    }
  }
})